/*    This is the setup program for the ERIM Golf League - Rules 2000   */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "golf.h"
#include "write_master.h"
#include "compute_hcp.h"

void read_player_file(char[], int &, int &);
void compute_hcp(int);
void initRecordFields(int);
void read_player_init_record(FILE *, int);

// globals
teamsInfo_t teamsInfo[MAX_TEAMS];
course_t courseSettings;        // holds par and hole ranks for scoring the round
individualMatch_t matchPlayers[NO_PLAYERS_PER_MATCH];
teamMatchHeader_t teamMatchHeader;
matchFileHeaderRecord_t matchFileHeader;
player_t players[MAX_PLAYERS + 1];  /* use the players id's as index.  The
                                       zero element of this vector is ignored */
int main(int argc, char *argv[])
{

    /*
       char player_file_name[MAX_FILENAME_LEN];
       char master_file_name[MAX_FILENAME_LEN];

       when debugging uncomment this assignments are comment out the arg inputs 
       char player_file_name = "c:\\golf\\00season\\00players";
       char master_file_name = "c:\\golf\\00season\\00master";
     */

    char *player_file_name;
    char *master_file_name;

    int nteams = 0;             /* number of league teams */
    int nsubs = 0;              /* Number of registered subs */
    int total_players = 0;      /* teams members + subs */

    if (argc != 3) {
        printf
            ("ERROR:Setup: Not the correct number of command line arguments \n");
        exit(-1);
    } else                      // assign file names from command line to pointers
    {
        player_file_name = argv[1];
        master_file_name = argv[2];
    }

    read_player_file(player_file_name, nteams, nsubs);

    total_players = (nteams * 2) + nsubs;

    initRecordFields(total_players);

    compute_hcps(total_players);

    write_master_file(master_file_name, nteams, total_players);

    return (0);

}                               /* end of program */

//#include "write_master.cpp"

//#include "compute_hcp.cpp"

void initRecordFields(int total_players)
{
    int i, j;

    for (i = FIRST_PLAYER; i <= total_players; i++)
        for (j = 0; j < MAX_RNDS; j++) {
            players[i].all_scores[j] = NO_SCORE;
            players[i].netScores[j] = NO_NET_SCORE;
            players[i].putts[j] = NO_PUTTS;
            players[i].matchPointsWon[j] = NO_PTS;
            players[i].whichNinePlayed[j] = NO_NINE;
            players[i].subNumber[j] = NO_SUB;
            players[i].numHolesPlayed[j] = NO_HOLES_PLAYED;
        }

    return;
}                               /* end of function initRecordFields  */

void read_player_file(char player_file_name[], int &nteams, int &nsubs)
{
    FILE *player_file;
    int fields_read = 0;        /* used to validate fscanf reads the corrent number of 
                                   fields */
    char temp_buf[30];
    int i;

    /* open the player file */
    if ((player_file = fopen(player_file_name, "r")) == NULL) {
        printf
            ("ERROR:read_player_file:opening file player initialization file: %s \n",
             player_file_name);
        return;
    } else
        printf(" file %s is open for reading \n", player_file_name);

    /* The first thing in the player file should be the number of league teams. */
    if ((fields_read = fscanf(player_file, "%s %d", temp_buf, &nteams)) != 2) {
        printf("ERROR:read_player_file:reading the number of teams\n");
        fclose(player_file);
        return;
    } else if ((strcmp(temp_buf, "Nteams")) != 0) {
        printf
            ("ERROR:read_player_file:First string read not equal to the string "
             "Nteams" " \n");
        exit(-1);
    }

    /* read in the player records for the offical team members */
    for (i = 0; i < nteams * 2; i++)
        read_player_init_record(player_file, (i + 1));

    /* After the team members records we read the subs */
    if ((fields_read = fscanf(player_file, "%s %d", temp_buf, &nsubs)) != 2) {
        perror("ERROR:read_player_file:Error reading the number of subs");
        fclose(player_file);
        return;
    } else if ((strcmp(temp_buf, "Subs")) != 0) {
        printf
            ("ERROR:read_player_file:string read not equal to the string "
             "Subs" " \n");
        exit(-1);
    }

    /* read in the player records for the subs */
    for (i = (nteams * 2); i < ((nteams * 2) + nsubs); i++)
        read_player_init_record(player_file, (i + 1));

    fclose(player_file);

    return;
}                               /* end of read_player_file  */

void read_player_init_record(FILE * fp, int record_num)
{
    int fields_read;
    int i;

    if ((fields_read = fscanf(fp, "%d %s %c",
                              &(players[record_num].id_number),
                              &(players[record_num].name[0]),
                              &(players[record_num].initial)
         )) != 3) {
        printf("ERROR:read_player_init_record:id,name,initial %d \n",
               record_num);
        exit(-1);
    } else {
        for (i = 0; i < HCP_WINDOW_LEN; i++)
            if ((fields_read =
                 fscanf(fp, "%f", &players[record_num].ESCAdjustedScores[i]))
                != 1) {
                printf
                    ("ERROR:read_player_init_record:ESCAdjustedScore %d %d\n",
                     players[record_num].id_number, i);
                exit(-1);
            }
    }

    return;

}                               /* end of function read_player_init_record  */
